package com.carl.mall.controller;


import com.carl.common.vo.MemberVO;
import com.carl.mall.interceptor.AuthInterceptor;
import com.carl.mall.service.ICartService;
import com.carl.mall.vo.Cart;
import com.carl.mall.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {


    @Autowired
    private ICartService cartService;

    @GetMapping("/cart_list")
    public String queryList(Model model){
        MemberVO memberVO = AuthInterceptor.threadLocal.get();
        Cart cart =  cartService.getCartList();
        model.addAttribute("cart",cart);
        System.out.println(memberVO);
        return "cartList";

    }

    /**
     * 加入购物车  item.html页面跳转到sucess.html页面
     * @param skuId skuId
     * @Param num  商品数量
     * @return
     */
    @RequestMapping("/addCart")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("num") Integer num, Model model) throws ExecutionException, InterruptedException {
        //DONE 将商品加入购物车的行为
        CartItem item = cartService.addCart(skuId,num);
        model.addAttribute("item",item);
        return "sucess";
    }

}
